//
//  GYIntegralViewController.m
//  GoodLuck
//
//  Created by apple on 2018/3/5.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYIntegralViewController.h"
#import "GYPoints.h"
#import "GYNetWorkTools.h"
#import "GYPointsCell.h"
#import "GYPointsRuleView.h"
#import "GYSearchViewController.h"
#import "GYBasePopoverViewController.h"
#import "PopoverAnimation.h"
#import "GYSystemMessageViewController.h"

@interface GYIntegralViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *markLableLeftMarginCons;

@property (weak, nonatomic) IBOutlet UIButton *detailsBtu;
@property (weak, nonatomic) IBOutlet UIButton *exchangeBtu;
@property (weak, nonatomic) IBOutlet UIButton *ruleBtu;

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *sumPointLable;

/** 积分规则视图 */
@property (nonatomic, strong) GYPointsRuleView *ruleView;


/** 数据源 */
@property (nonatomic, strong) NSMutableArray *pointsList;

@property (nonatomic,getter=isHasmore) BOOL hasmore;

@property (nonatomic, assign) NSInteger currentPage;

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableviewBottomMarginCons;

@end

@implementation GYIntegralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentPage = 1;
    self.hasmore = YES;
    self.tableviewBottomMarginCons.constant = iPhoneX ? 44 : 0;
    
    //初始化
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"积分明细";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(rightButClick) button:nil];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //加载积分总数
    [self loadUserProperty];
    
    //加载数据
    [self loadPointsDetails:@"1"];
}

- (void)loadMoreData {
    [self.tableView.mj_footer endRefreshing];
    [self loadPointsDetails:[NSString stringWithFormat:@"%zd",self.currentPage]];
}

- (void)setupPointsRuleView {
    GYPointsRuleView *pointsRultView = [[[NSBundle mainBundle] loadNibNamed:@"GYPointsRuleView" owner:nil options:nil] firstObject];
    pointsRultView.frame = self.bgView.frame;
    
    [self.view addSubview:pointsRultView];
    self.ruleView = pointsRultView;
}

- (void)rightButClick {
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                weakSelf.tabBarController.selectedIndex = 0;
                [weakSelf.tabBarController.tabBar setHidden:NO];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [self.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
                break;
                
            case 3:
            {
                GYSystemMessageViewController *systemMessageVC = [[GYSystemMessageViewController alloc] initWithNibName:@"GYSystemMessageViewController" bundle:nil];
                [weakSelf.navigationController pushViewController:systemMessageVC animated:YES];

            }
                break;
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"我的商城",@"消息"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"ff_icon4",@"message_w"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}

//加载总积分
- (void)loadUserProperty {
    [GYNetWorkTools requestUserProperty:^(NSDictionary *obj) {
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            NSString *point = [NSString stringWithFormat:@"%@",[datas objectForKey:@"point"]];
            self.sumPointLable.text = point;
        }else {
            NSLog(@"网络加载出错...");
        }
    } error:^(NSError *error) {
        NSLog(@"laodUserPropertyFailed=====%@",error);
    }];
}

//加载积分详情.
- (void)loadPointsDetails:(NSString *)curpage {
    if (self.isHasmore) {
        [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
            EasyLodingConfig *config = [EasyLodingConfig shared];
            config.showOnWindow = YES;
            
            return config;
        }];
        [GYNetWorkTools requestPointsDetails:curpage page:@"20" success:^(NSDictionary *obj) {
            //NSLog(@"obj=========%@",obj);
            int code = [[obj objectForKey:@"code"] intValue];
            NSDictionary *datas = [obj objectForKey:@"datas"];
            if (code == 200) {
                [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                self.currentPage += 1;
                self.hasmore = [[obj objectForKey:@"hasmore"] boolValue];
                NSArray *log_list = [datas objectForKey:@"log_list"];
                [self.pointsList addObjectsFromArray:[GYPoints loadPointsDetails:log_list]];
                [self.tableView reloadData];
                
            }else if (code == 400) {
                [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                [EasyTextView showText:@"加载出错,请稍后加载!"];
            }
        } error:^(NSError *error) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载出错,请稍后加载!"];
            NSLog(@"laodPointsDetailsFailed====%@",error);
        }];
    }else {
        [EasyTextView showText:@"没有更多数据了!"];
        NSLog(@"没有数据加载....");
    }
}

#pragma mark -- 按钮点击事件

- (IBAction)onClick:(UIButton *)sender {
    
    //改变标记lable的位置
    [UIView animateWithDuration:0.5 animations:^{
        self.markLableLeftMarginCons.constant = sender.tag *10 + (sender.tag-1)*(ScreenWidth-40)/3;
    }];
    switch (sender.tag) {
        case 1:
        {
            [self.detailsBtu setTitleColor:RGBColor(68, 165, 25) forState:UIControlStateNormal];
            [self.exchangeBtu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.ruleBtu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            self.bgView.hidden = NO;
            self.ruleView.hidden = YES;
        }
            break;
            
        case 2:
        {
            [self.detailsBtu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.exchangeBtu setTitleColor:RGBColor(68, 165, 25) forState:UIControlStateNormal];
            [self.ruleBtu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            self.bgView.hidden = YES;
            self.ruleView.hidden = YES;
        }
            break;
            
        case 3:
        {
            [self.detailsBtu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.exchangeBtu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.ruleBtu setTitleColor:RGBColor(68, 165, 25) forState:UIControlStateNormal];

            self.bgView.hidden = YES;
            if (self.ruleView != nil) {
                self.ruleView.hidden = NO;
            }else {
                [self setupPointsRuleView];
            }
        }
            break;
            
        default:
            break;
    }
}


#pragma mark --  UITableViewDelegate,UITableViewDataSource 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pointsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYPointsCell *cell = [GYPointsCell cellWithTbaleView:tableView];
    cell.points = self.pointsList[indexPath.row];
    
//    if (indexPath.row == self.pointsList.count-1) {
//        [self loadPointsDetails:[NSString stringWithFormat:@"%zd",self.currentPage]];
//    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}



- (void)dealloc {
    NSLog(@"GYIntegralViewController ====dealloc");
}

- (NSMutableArray *)pointsList {
    if (!_pointsList) {
        _pointsList = [NSMutableArray array];
    }
    
    return _pointsList;
}
@end
